구구단
dan = int(input("원하는 단은:"))
for i in range(1,10):
    value = dan * i
    print("{}*{}={}".format(dan,i,value) )
 
ValueError: invalid literal for int() with base 10: ''
 
 
1부터 10까지의 합 계산
num = 1
total = 0
while num<=10:
    total = total + num
    num+=1
print(total)
 
 
사용자가 입력하는 숫자의 합 계산
total = 0
inpt = "yes"
while inpt == "yes":
    total = total + int(input("숫자를 입력하시오"))
    inpt = input("계속?(yes/no):")
print("합계는: {}".format(total))
 
 
숫자 맞추기 게임
import random
print("1부터 100 사이의 숫자를 맞추시오")
num = random.randint(1,100)
inpt = 0
count = 0
while inpt != num:
    inpt = int(input("숫자를 입력하시오:"))
    count+=1
    if inpt < num:
        print("낮음")
    elif inpt > num:
        print("높음")
print("축하합니다. 시도횟수={}".format(count))        
 
 
자음과 모음의 개수 카운팅하기
def countVowel(string): 
    count = 0
    for ch in string:
        if ch in ['a','e','i','o','u']: 
            count += 1
    return count
s = input("문자열을  입력하시오: ") 
n = countVowel(s)
print(f"모음의 개수는 {n}개입니다.")
 
 
지역변수 전역변수 개념
- 지역변수?->함수 안에서 선언 + 할당,함수안에서만 사용 가능함. 함수가 종료되면 소멸
 
- 전역변수?->함수 밖에서 선언 + 할당,함수안팎에서 모두 사용가능함. 사라지지 않아요
 
- 함수안에서 전역변수를 선언 + 할당하려면?
- Glboal로 함수안에서 먼저 선언
 
- 그 후 =로 할당
 
 
def calculate_area(radius):
    global area
    area = 2 * 3.14 * radius
    return 
r = float(input("원의 반지름:"))
calculate_area(5)
print(area)
 
 
keyword argument vs positional argument
- keyword argument와 positional argument는 매개변수(parameter)에 인수(argument)를 전달하는 
방법이다.
- keyword argument : 함수 호출 시, 인수를 전달받을 파라미터를 명시적으로 알려줌으로서 인수를 파라미터에 전달하는 방법.
 
- positional argument : 함수 호출 시,인수를 전달받을 파라미터의 위치에 맞게 인수를 넣어줌으로서 인수를 파라미터에 전달하는 방법.
 
 
def calc(x,y,z):
    return x+y+z
 
 
answer = calc(x=3,y=4,z=5)
print(answer)
 
 
answer = calc(3,4,5)
print(answer)
 
 
안되는 것 : 키워드 인수 뒤에 위치 인수가 나올 수 없다.
answer = calc(x=3,y=4,5)
print(answer)
 
SyntaxError: positional argument follows keyword argument (1146439424.py, line 1)
 
 
리스트,튜플,딕셔너리,세트
myList = [10,20,30,40,50]
myTuple = (10,20,30,40,50)
myDict = {1:"one",2:"two",3:"three"}
mySet = {"one","two","three"}
 
 
myList.append(60)
print(myList)
 
 
myList.insert(3,35)
print(myList)
 
[10, 20, 30, 35, 40, 50, 60]
 
 
myList.extend(range(10))
myList
 
[10, 20, 30, 35, 40, 50, 60, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 
 
값 삭제
[20, 30, 35, 40, 50, 60, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 
 
값 삭제 후 반환(꺼내기)
d = myList.pop(-1)
print(d,myList)
 
9 [20, 30, 35, 40, 50, 60, 0, 1, 2, 3, 4, 5, 6, 7, 8]
 
 
myList.clear()
print(myList)
 
 
{1: 'one', 2: 'two', 3: 'three'}
 
 
d = myDict.pop(1)
print(d,myDict)
 
one {2: 'two', 3: 'three'}
 
 
c = myDict.popitem()
print(c,myDict)
 
 
myDict.clear()
print(myDict)
 
 
names = ["호연","수연"]
d = names.pop()
print(d,names)
 
 
myList = ["우유","사과","두부","소고기"]
myList.remove("사과")
myList
 
 
합치기
['호연', '우유', '두부', '소고기']
 
 
myList = myList * 2
myList
 
['우유', '두부', '소고기', '우유', '두부', '소고기']
 
 
['우유', '두부', '소고기', '우유', '두부', '소고기']
 
 
.sort 메서드는 원 리스트 자체를 변경
nums = [9,6,7,1]
nums.sort()
nums
 
 
sorted함수는 iterable이 input 정렬된 객체가 output
 원래 객체는 그대로 라는 사실 유의
 바꿔주려면 다시 할당해야함
 default : 오름차순 reverse = True : 내림차순
nums = [9,6,7,1]
newlist = sorted(nums)
newlist2 = sorted(nums,reverse = True)
nums,newlist,newlist2
 
([9, 6, 7, 1], [1, 6, 7, 9], [9, 7, 6, 1])
 
 
이차함수 그래프 그리기
import matplotlib.pyplot as plt
xlist=  []
ylist = []
a = int(input("2차항의 계수"))
b = int(input("1차항의 계수"))
c = int(input("상수항"))
for x in range(-100,100,):
    y = a*x**2 + b*x + c
    xlist.append(x)
    ylist.append(y)
plt.plot(xlist,ylist)
plt.show()